package com.example.myapplication.adapter.recyclerViewAdapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.activity.BaseOnclickListener;
import com.example.myapplication.R;
import com.example.myapplication.entity.AlbumDetail;
import com.example.myapplication.entity.PlayListDetail;

import java.util.List;

public class AlbumDetailAdapter extends RecyclerView.Adapter<AlbumDetailAdapter.AlbumDetailHolder>{
    private Context context;
    private BaseOnclickListener baseOnclickListener;
    private List<AlbumDetail.SongsBean>songsBeanList;
    private PlayListDetail.PlaylistBean playlistBean;



    public void setData(List<AlbumDetail.SongsBean>songsBeanList){
        this.songsBeanList = songsBeanList;
        notifyDataSetChanged();
    }
    public void setData(PlayListDetail.PlaylistBean playlistBean){
        this.playlistBean = playlistBean;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public AlbumDetailHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_hotsong,parent,false);
        return new AlbumDetailHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AlbumDetailHolder holder, int position) {
        if(songsBeanList!=null) {
            AlbumDetail.SongsBean songsBean = songsBeanList.get(position);
            holder.name_hotsong.setText(songsBean.getName());
            holder.singer.setText(songsBean.getAr().get(0).getName());
        }
        else if(playlistBean!=null){
            PlayListDetail.PlaylistBean.TracksBean tracksBean= playlistBean.getTracks().get(position);
            holder.name_hotsong.setText(tracksBean.getName());
            holder.singer.setText(tracksBean.getAr().get(0).getName()+"-"+tracksBean.getAl().getName());
        }
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(baseOnclickListener !=null){
                    baseOnclickListener.onclick(position);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        if(songsBeanList==null&&playlistBean==null){
            return 0;
        }
        else if(songsBeanList==null){
            return playlistBean.getTracks().size();
        }
        else {
            return songsBeanList.size();
        }
    }

    public void setBaseOnclickListener(BaseOnclickListener baseOnclickListener){
        this.baseOnclickListener = baseOnclickListener;
    }


    class AlbumDetailHolder extends RecyclerView.ViewHolder {
        TextView name_hotsong;
        View view;
        TextView singer;
        public AlbumDetailHolder(@NonNull View itemView) {
            super(itemView);
            name_hotsong = itemView.findViewById(R.id.name_hotsong);
            view = itemView.findViewById(R.id.view);
            singer = itemView.findViewById(R.id.singer);
        }
    }
}
